package com.ty.init.controller;

import com.ty.init.controller.ex.CaptchaNotMatchException;
import com.ty.init.service.impl.ex.PasswordNotMatchException;
import com.ty.init.service.impl.ex.UserExistException;
import com.ty.init.service.impl.ex.UserNotFindException;
import com.ty.init.uitl.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;
import java.rmi.server.ServerCloneException;

/**
 * @author ty
 */
public class BaseController {
    public final static Integer OK = 0;
    @ExceptionHandler(ServerCloneException.class)
    public JsonResult<Void> handleException (Throwable e){
        JsonResult result = new JsonResult<>(e);
        if (e instanceof PasswordNotMatchException){
            result.setMessage("密码错误");
            result.setCode(4000);
        }if (e instanceof UserNotFindException){
            result.setMessage("用户不存在");
            result.setCode(4001);
        }if (e instanceof CaptchaNotMatchException){
            result.setMessage("验证码不正确");
            result.setCode(4002);
        }if (e instanceof UserExistException){
            result.setMessage("用户已存在");
            result.setCode(4003);
        }
        return result;
    }
    public final Integer getUidFromSession(HttpSession session){
        return Integer.valueOf(session.getAttribute("uid").toString());
    }
    public final String getUsernameFromSession(HttpSession session){
        return session.getAttribute("username").toString();
    }
}
